package com.FengChaoSystem.servlet;

import com.FengChaoSystem.pojo.User;
import com.FengChaoSystem.service.UserImpl;
import com.FengChaoSystem.service.UserService.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/getImgServlet")
public class GetImgServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    String proPath="D:\\Code\\ChenXufeng_Fengchao_System\\src\\main\\webapp\\head_img\\";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行了");
        HttpSession session = req.getSession();
        String id = req.getParameter("id");
        UserImpl service=new UserService();
        User temp=new User();
        temp.setId(1);
        User user = service.getImg(temp);
        System.out.println("获取的图象的路径为:"+user.getImagPath());
        ObjectMapper mapper = new ObjectMapper();
        resp.setContentType("application/json;charset=utf-8");
        mapper.writeValue(resp.getOutputStream(), "http://localhost:80/head_img/"+user.getImagPath());
        System.out.println(proPath+user.getImagPath());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了");
        UserImpl service=new UserService();
        User temp=new User();
        temp.setId(Integer.parseInt(req.getParameter("id")));
        temp.setId(1);
        User user = service.getImg(temp);
        String path= user.getImagPath();
        System.out.println("获取的图象的路径为:"+path);
//        ObjectMapper mapper = new ObjectMapper();
//        resp.setContentType("application/json;charset=utf-8");
//        mapper.writeValue(resp.getOutputStream(),user.getImagPath());
//        String jsonString = JSON.toJSONString(user.getImagPath());
//        response.setContentType("text/json;charset=utf-8");
//        response.getWriter().write(jsonString);

        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
            out.print(path);

        out.flush();
        out.close();

    }
}
